#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=5005;

int a[N][N];
int s[N][N];

void solve(){
    int n,r;
    cin>>n>>r;
    while(n--){
        int x,y,v;
        cin>>x>>y>>v;
        a[x+1][y+1]+=v;
    }
    for(int i=1;i<N;i++){
        for(int j=1;j<N;j++){
            s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
        }
    }
    int maxn=0;
    for(int i=1;i<N;i++){
        for(int j=1;j<N;j++){
            int x2=max(1,i-r+1);
            int y2=max(1,j-r+1);
            int x1=i;
            int y1=j;
            int sum=s[x1][y1]-s[x2-1][y1]-s[x1][y2-1]+s[x2-1][y2-1];
            if(sum>maxn)maxn=sum;
        }
    }
    cout<<maxn<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}